package com.xtxb.app.rmq.api;

import com.xtxb.app.rmq.consume.ConsumeServer;
import com.xtxb.app.rmq.produce.SenderMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.ws.rs.*;
import java.util.*;

@Component
@Path("/consume")
public class ConsumeAPI extends AbtractAPI{
    @Autowired
    private ConsumeServer server;
    @Autowired
    private SenderMessage sender;

    @POST
    @Path("/start")
    @Produces("application/json")
    public boolean startConsume(@QueryParam("uuid")String uuid){
        return server.startConsume(uuid);
    }

    @POST
    @Path("/stop")
    @Produces("application/json")
    public boolean stopConsume(@QueryParam("uuid") String uuid){
        return server.stopConsume(uuid);
    }

    @GET
    @Path("/list")
    @Produces("application/json")
    public List<String> getSubscribed(){
        List<String> values=new ArrayList<>();
        for(Map<String,String> map:server.getCconsumerTopics().values()){
            values.addAll(map.values());
        }
        return values;
    }

    @POST
    @Path("/send")
    @Consumes("application/json")
    public boolean sendMsg(@RequestBody Map<String,String> message ){
        return sender.sendMsg(message.get("serverName"),message.get("groupName"),message.get("topic"),message.get("tag"),message.get("message"));
    }
}
